<?php
ini_set('error_reporting', 6143);
ini_set('display_errors','1');
define('IN_LC', true);
define('MM_ID', 2);

include_once("validate.php");
include_once("includes/style.php");
include_once("includes/champ_details.php");
include_once("includes/addMessage.php");


$done = false;
// are we showing an actual profile?
if(isset($_GET['id']))
{
    $enable_check = " AND c_enabled = 1 ";

    if(isset($_SESSION['valid_admin']) && $_SESSION['valid_admin'] >= 2)
        $enable_check = "";

    $userextra = '';

    if(isset($_SESSION['valid_id']) && is_numeric($_SESSION['valid_id']))
    {
        $userextra = '(SELECT cs_driver FROM '. CHAMPSIGNUPS_TABLE . '
                  WHERE cs_champ = c_id AND cs_driver = ' . $_SESSION['valid_id'] . ') as champentered,
                 (SELECT l_order FROM ' . LICENSES_TABLE . ', ' . LCUSERS_TABLE . '
                  WHERE u_license = l_id AND u_id = ' . $_SESSION['valid_id'] .') as userlicense,';
    }
    // Create query
    $q = "SELECT *,{$userextra}
                 date_format(c_firstRace, '%M %e, %Y') as c_firstRaceF,
                 date_format(c_lastRace, '%M %e, %Y') as c_lastRaceF,
                 (SELECT COUNT(*) FROM " . RACERESULTS_TABLE .", ". EVENTS_TABLE ." WHERE r_event = e_id AND e_championship = c_id) as result_count,
                 (SELECT COUNT(*) FROM " . RESERVEDNUMBERS_TABLE . " WHERE r_champ = " .$_GET['id'] . ") AS resno_count,
                 (SELECT COUNT(*) FROM " . RESERVEDDIVISIONS_TABLE . " WHERE r_champ = " .$_GET['id'] . ") AS resdiv_count
         FROM ". CHAMPIONSHIPS_TABLE .", " . LICENSES_TABLE . ", " . GAMES_TABLE . "
         WHERE c_license = l_id {$enable_check} AND c_game = g_id AND c_id = ". $_GET['id'];


    // Run query
    $res = $db->sql_query($q);
    if ( $row = $db->sql_fetchrow($res)  )
    {
            $style = new style(true);
            include_once('includes/side_menu.php');
            $style->addRenderLeft(new side_menu('championship', $_GET['id'], $row));
            if(isset($_GET['mode']))
            {
                    $champ = new champ_details($row, $_GET['mode']);
            }
            else
            {
                    $champ = new champ_details($row, 'intro');
            }
            $style->addRenderRight($champ);
            $style->render();
            $done = true;
    }
}

if(!$done)
{
	include_once('includes/renderInclude.php');
	
	if(isset($_GET['p']))
	{
		$page = $_GET['p'];
		$start =($page * 10) - 10;
	}
	else
	{
		$page = 1;
		$start = 0;
	}
	
	$q = 'SELECT COUNT(*) as row_count FROM ' . CHAMPIONSHIPS_TABLE;
	$res = $db->sql_query($q);
	if ( $row = $db->sql_fetchrow($res)  )
	{
		$row['row_count'] = ceil($row['row_count'] / 10);
		$text = '';
		for($x = 1; $x <= $row['row_count']; $x++)
		{
			if($page == $x)
			{
				$text .= $x . " | ";
			}
			else
			{
				$text .= "<a href='championships.php?p=" . $x . "'>" . $x . "</a> | ";
			}
		}
		
		if($text != '')
		{
			$text = substr($text, 0 , -2);
		}
		$text = $lang['ChampionshipText'] . '<br><br>' . $lang['Page'] . ': ' . $text;
		
	}
	else
	{
		$text = $lang['ChampionshipText'];
	}

        $userextra = '';

        if(isset($_SESSION['valid_id']) && is_numeric($_SESSION['valid_id']))
        {
            $userextra = '(SELECT cs_driver FROM '. CHAMPSIGNUPS_TABLE . '
                      WHERE cs_champ = c_id AND cs_driver = ' . $_SESSION['valid_id'] . ') as champentered,
                     (SELECT l_order FROM ' . LICENSES_TABLE . ', ' . LCUSERS_TABLE . '
                      WHERE u_license = l_id AND u_id = ' . $_SESSION['valid_id'] .') as userlicense,';
        }

        // Create query
        $q = "SELECT *, " . $userextra . " date_format(c_firstRace, '%M %e, %Y') as c_firstRaceF, date_format(c_lastRace, '%M %e, %Y') as c_lastRaceF
              FROM ".  CHAMPIONSHIPS_TABLE .", " .  LICENSES_TABLE . ", " . GAMES_TABLE . "
              WHERE c_license = l_id AND c_enabled = 1 AND c_game = g_id ORDER BY c_lastRace DESC LIMIT " . $start . ', 10';


	$style = new style(false);				
	$style->addRenderLeft(new addMessage($text,$lang['Championships']));
	// Run query
	$res = $db->sql_query($q);
	$style->addRenderLeft(new renderInclude('tpl/tableStart.tpl'));
	while ( $row = $db->sql_fetchrow($res)  )
	{
		$style->addRenderLeft(new champ_details($row, 'Mini'));
	}
	$style->addRenderLeft(new renderInclude('tpl/tableEnd.tpl'));
	$style->render();
}

echo $db->num_queries;
?>